//
//  BBAppController.m
//  BBCoverFlowTest
//
//  Created by ben smith on 1/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBAppController.h"
#import "BBCoverFlowView.h"
#import "BBImageView.h"
#import "BBManipulateView.h"
#import "BBDispatchViewDelegate.h"
#import "BBOSC.h"
#import <QuartzCore/QuartzCore.h>
#import "BBGradientBackView.h"

@implementation BBAppController

@synthesize dispatchDelegate;
@synthesize oscListener;
@synthesize coverFlowView;
@synthesize lightboxView;


BBAppController* sharedAppController;

+(BBAppController*)sharedAppController
{
	return sharedAppController;
}

-(void)awakeFromNib
{
	sharedAppController = self;
	[NSApp setDelegate:self];	
	[self setDefaults];
}

-(void)setDefaults
{
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBMaxDimension"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:350.0 forKey:@"BBMaxDimension"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBFlowSpacing"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:50.0 forKey:@"BBFlowSpacing"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBNewmanFactor"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:420.0 forKey:@"BBNewmanFactor"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBNewmanAngle"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:0.79 forKey:@"BBNewmanAngle"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBFlowVerticalOffset"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:100.0 forKey:@"BBFlowVerticalOffset"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBButtonVerticalOffset"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:100.0 forKey:@"BBButtonVerticalOffset"];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBShowShadowsFlag"] == nil) 
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"BBShowShadowsFlag"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBHighRezScaleFactor"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:1.5 forKey:@"BBHighRezScaleFactor"];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBShadowOpacity"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:0.6 forKey:@"BBShadowOpacity"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBFlingFactor"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:10.0 forKey:@"BBFlingFactor"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBFlowFlipDistance"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:85.0 forKey:@"BBFlowFlipDistance"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBSwipeThreshold"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:200.0 forKey:@"BBSwipeThreshold"];

	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBTapThreshold"] == nil) 
		[[NSUserDefaults standardUserDefaults] setFloat:10.0 forKey:@"BBTapThreshold"];

	
	
	
	[[NSUserDefaults standardUserDefaults] floatForKey:@""];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{	
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shuffleViews:) name:@"BBShuffleViewsNotification" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(quitAndLaunch:) name:@"BBQuitAndLaunch" object:nil];
	
	
	holderView = [[BBGradientBackView alloc] initWithFrame:[[mainWindow contentView] bounds]];
	[holderView setAutoresizesSubviews:YES];
	[[mainWindow contentView] addSubview:holderView];
	
	//lightboxView = [[BBManipulateView alloc] initWithFrame:[[mainWindow contentView] bounds]];
	
	//coverFlowView = [[BBCoverFlowView alloc] initWithFrame:[[mainWindow contentView] bounds]];

	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBShuffleViewsNotification" object:[[NSUserDefaults standardUserDefaults] stringForKey:@"BBPhotoDirectory"]];	
	
	if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BBStartFullscreenFlag"])
		[self fullscreen:self];
	[self performSelector:@selector(connect) withObject:nil afterDelay:1.0];
}


-(void)quitAndLaunch:(NSNotification*)note
{
	NSString * otherApp = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBExternalAppLocation"];
	if (otherApp != nil) {
		if ([[NSWorkspace sharedWorkspace] launchApplication:otherApp]) {
		}			
	}
	[NSApp terminate:self];
}

-(void)shuffleViews:(NSNotification*)note
{
	NSString* dirname = [note object];
	if (dirname == nil) return;
	NSString * zeroDir = [[NSUserDefaults standardUserDefaults] stringForKey:@"BBPhotoDirectory"];
	// figure out the depth
	NSInteger zeroCount = [[zeroDir pathComponents] count];
	NSInteger thisCount = [[dirname pathComponents] count];
	NSInteger depth = thisCount - zeroCount;
	BBImageView * aView;
	if ([[holderView subviews] containsObject:coverFlowView]) {
		// switch to the lightboxView
		[coverFlowView removeFromSuperview];
		self.lightboxView = [[BBManipulateView alloc] initWithFrame:[holderView bounds]];
		aView = lightboxView;
		[lightboxView release];
	} else {
		//switch to coverFlowView
		[lightboxView removeFromSuperview];
		self.coverFlowView = [[BBCoverFlowView alloc] initWithFrame:[holderView bounds]];
		aView = coverFlowView;
		[coverFlowView release];
	}

	aView.directoryDepth = depth;
	[aView setFrame:[holderView bounds]];
	[holderView addSubview:aView];
	
	
	NSArray * fileNames = [[NSFileManager defaultManager] directoryContentsAtPath:dirname];
	NSMutableArray * fullPaths = [NSMutableArray array];
	for (NSString * name in fileNames) {
		if ([name hasPrefix:@"."]) continue;
		[fullPaths addObject:[dirname stringByAppendingPathComponent:name]];
	}
	if (depth > 0) [fullPaths removeObjectAtIndex:0];
	[aView addImageFiles:fullPaths];

}

-(IBAction)browseForImageDirectory:(id)sender
{
	int result;
	NSOpenPanel *oPanel = [NSOpenPanel openPanel];
	
	[oPanel setAllowsMultipleSelection:NO];
	[oPanel setCanChooseFiles:NO];
	[oPanel setCanChooseDirectories:YES];

	NSString * key = @"BBPhotoDirectory";
	if ([sender tag] == 1) {
		key = @"BBExternalAppLocation";		
		[oPanel setCanChooseFiles:YES];
		[oPanel setCanChooseDirectories:NO];
	}
	
	result = [oPanel runModalForDirectory:NSHomeDirectory()
																	 file:nil types:nil];

	
	if (result == NSOKButton) {
		NSArray *filesToOpen = [oPanel filenames];
		NSString *aFile = [filesToOpen objectAtIndex:0];
		[[NSUserDefaults standardUserDefaults] setObject:aFile forKey:key];
	}
}


-(IBAction)fullscreen:(id)sender
{
	if (![holderView isInFullScreenMode])
		[holderView enterFullScreenMode:[[holderView window] screen] withOptions:nil];
	else
		[holderView exitFullScreenModeWithOptions:nil];
}

-(void)connect
{
	BBDispatchViewDelegate * newDelegate = [[BBDispatchViewDelegate alloc] init];
	[newDelegate setMainView:holderView];
	self.dispatchDelegate = newDelegate;
	[newDelegate release];
	
	// make a new listener, point it at the specified port
	// and hook it up to the dispatch delegate
	
	BBOSCListener * newListener = [[BBOSCListener alloc] init];
	
	[newListener setDelegate:self.dispatchDelegate];
	[newListener startListeningOnPort:3333];
	// keep a copy for myself
	self.oscListener = newListener;
	
	// clean up
	[newListener release];
}


@end
